package com.bio.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

/**
 * 这个类包含一些与web相关的util方法
 *
 */
public class WebUtils {
    private WebUtils() {}
    
    /**
     * 添加一个name=value到给定的url.
     * @param url   要添加参数的url
     * @param paramName 参数名字
     * @param paramValue 参数的值
     * @return 添加了参数的url
     */
    public static String addParam(String url, String paramName, String paramValue) {
        StringBuffer result = new StringBuffer();
        if (url != null) {
            result.append(url);
            if (url.indexOf("?") >= 0) {
                result.append("&");
            } else {
                result.append("?");
            }
            result.append(paramName)
                  .append("=")
                  .append(paramValue);
        }
        
        return result.toString();
    }
    
    /**
     * 把给定url用utf-8编码编成application/x-www-form-urlencoded的格式。
     * @param url 要编码的url
     * @return 编码过的url
     */
    public static String encodeUrl(String url) {
        try {
            url = URLEncoder.encode(url, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        
        return url;
    }
    
    public static String getHostPath(HttpServletRequest request) {
        String hostname = request.getServerName();
        int port = request.getServerPort();
        StringBuffer hostPath = new StringBuffer();
        
        hostPath.append(request.getScheme())
                .append("://")
                .append(hostname);
        
        // add port
        if (port != 80) {
            hostPath.append(":")
                    .append(port);
        }
        
        // Add context path
        if (!request.getContextPath().isEmpty()) {
            hostPath.append(request.getContextPath());
        }
        
        return hostPath.toString();
    }
    
    public String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
    
      /**  
         * 删除input字符串中的html格式  
         *   
         * @param input  
         * @param length  
         * @return  
         */  
        public static String splitAndFilterString(String input, int length) {   
            if (input == null || input.trim().equals("")) {   
                return "";   
            }   
            // 去掉所有html元素,   
            String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(   
                    "<[^>]*>", "");   
            str = str.replaceAll("[(/>)<]", "");   
            int len = str.length();   
            if (len <= length) {   
                return str;   
            } else {   
                str = str.substring(0, length);   
                str += "...";   
            }   
            return str;   
        }  
        /**
         * 去掉标签后全部返回
         * @param input
         * @return
         */
        public static String splitAndFilterString(String input) { 
            if (input == null || input.trim().equals("")) {   
                return "";   
            }
            // 去掉所有html元素,   
            String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(   
                    "<[^>]*>", "");   
            str = str.replaceAll("[(/>)<]", "");
            
            return str;
        }
        public static void main(String[] args) {
            String r = "<div>我是刘玉<span style='color:red'>你不想看看嘛</span></div>";
            String res = splitAndFilterString(r,50);
            System.out.println(res);
        }

}
